#################################################################
##                           Table 5                           ##
#################################################################


# Load & clean World Values Survey Wave 6 Data --------------------------------------

# Load the WVS data with selected variables
df_wvs <- readRDS("07_data_wvs_wave_6.rdata") %>%
  rename(
    countrycode = V2,
    year = V262,
    sex = V240,
    interest = V84
  )

# Create COW codes from country names
df_wvs$cowcode <- countrycode(df_wvs$countrycode, "wvs", "cown")

# Check to see which cases don't have COW codes
df_wvs %>% filter(is.na(cowcode)) %>%  tabyl(countrycode)

# Drop cases where COW code == NA (Palestine = 275; Hong Kong = 344)
df_wvs <- filter(df_wvs, !is.na(cowcode))

# Create a dummy variable coding whether the respondent expresses *at least
# some* interest in politics. Note: In the original code, 1 = "Very interested",
# 2 = "Somewhat interested", 3 = "Not very interested" and 4 is "not at all
# interested". 

# Then, create a country-specific variable indicating the share of
# women with an interest in politics (i.e., the share of women with "1" on the
# above dummy variable). 

df_wvs <- df_wvs %>%  
  mutate(some_interest_dum = ifelse(interest == 1 | interest == 2, 1, 0)) %>%  
  group_by(cowcode, countrycode) %>% 
  mutate(some_interest_share = sum(some_interest_dum, na.rm = TRUE) / n())

# Now, create the country averages
df_wvs <- df_wvs %>% 
  group_by(cowcode, countrycode) %>% 
  summarize(across(where(is.numeric), function(x) mean(x, na.rm = TRUE))) %>% 
  ungroup()

# Add the relevant economic, social, and political data from df_tscs
df_wvs_augmented <- df_tscs %>%
  select(
    country,
    cowcode,
    year,
    log_wdi_overvalued,
    log_gdpcap,
    log_oilgascap,
    vdem_elec_dem,
    oecd,
    irr_rate_regime
  ) %>%  
  right_join(df_wvs, by = c("cowcode", "year"))


# Table 5: logOvervalued ~ Interest * Regime Type  ----------------------------------------------

lm_interest <- lm(
  log_wdi_overvalued ~ some_interest_share * vdem_elec_dem +
    log_gdpcap + log_oilgascap +
    vdem_elec_dem + factor(irr_rate_regime) - country,
  data = subset(df_wvs_augmented, oecd == 0)
)

# View the results with robust standard errors
coeftest(lm_interest, vcov. = vcovHC(lm_interest, type = "HC1"))

# Table 5: Make the table -----------------------------------------------------

# Create variable names
list_var_names_interest_texreg <- list(
  'log_wdi_overvalued'                = 'logOvervalued',
  'some_interest_share'               = 'Political Interest',
  'vdem_elec_dem'                     = 'Regime Type', 
  'some_interest_share:vdem_elec_dem' = 'Political Interest $\\times$ Regime Type',
  'log_gdpcap'                        = 'GDP',  
  'sqlog_gdpcap'                      = 'GDP$^2$',  
  'log_oilgascap'                     = 'Resource Rents',  
  'wdi_fertility'                     = 'Fertility',
  'factor(irr_rate_regime)2'          = 'Exchange Rate: Narrow Crawling',
  'factor(irr_rate_regime)3'          = 'Exchange Rate: Wide Crawling',
  'factor(irr_rate_regime)4'          = 'Exchange Rate: Freely Floating',
  'factor(irr_rate_regime)5'          = 'Exchange Rate: Freely Falling',
  'factor(irr_rate_regime)6'          = 'Exchange Rate: Dual Market',
  '(Intercept)'                       = 'Constant'
)

# make the table
tab_interest <- screenreg(
  lm_interest,
  override.se = coeftest(lm_interest, vcov. = vcovHC(lm_interest, type = "HC1"))[, 2],
  override.pvalues = coeftest(lm_interest, vcov. = vcovHC(lm_interest, type = "HC1"))[, 4],
  single.row = TRUE,
  stars = c(0.01, 0.05, 0.1),
  caption = "Women's Political Engagement, Democracy, and Exchange Rate Level",
  # Coefficient labels
  custom.coef.map = list_var_names_interest_texreg,
  # Goodness-of-fit statistics
  include.rsquared = FALSE,
  include.adjrs = FALSE,
  include.aic = FALSE,
  include.aiclm = FALSE,
  include.loglik = FALSE,
  include.lr = FALSE,
  # Custom notes
  custom.note = "%stars,
  Robust standard errors in parentheses"
)

